import { _decorator, Component, ModelComponent, Node, Material, Prefab, Color, Texture2D } from 'cc';
import { ResourceManager } from './ResourceManager';

const { ccclass, property } = _decorator;
@ccclass('SkinManager') // 用 @ccclass 声明类为 Cocos 组件
export class SkinManager extends Component {

    @property([Node])  desktopNodes: Node[] = [];
    @property([Node])  cardNode: Node | null = null;

	static instance: SkinManager;

    onLoad() {
        SkinManager.instance = this;
        const desktopIndex = JSON.parse(localStorage.getItem("DesktopSkin") || "0");
        const index = parseInt(desktopIndex);
        this.setDesktopSkin(index);
    }

    getDesktopSkin() {
        const index = parseInt(localStorage.getItem("DesktopSkin") || "0");
        return index;
    }

    setDesktopSkin(index:number) {

        localStorage.setItem("DesktopSkin", "" + index);

        let baseMat: Material = ResourceManager.getDesktopMat();
        const colors = [ "064C48", "144759", "564834", "47405D", ];
        const hexColor = colors[index];
        const r = parseInt(hexColor.slice(0, 2), 16);
        const g = parseInt(hexColor.slice(2, 4), 16);
        const b = parseInt(hexColor.slice(4, 6), 16);
        const color = new Color(r, g, b);
        baseMat.setProperty('mainColor', color);
    }

    getCardSkin() {
        const index = parseInt(localStorage.getItem("CardSkin") || "0");
        return index;
    }

    setCardSkin(index:number) {
        localStorage.setItem("CardSkin", "" + index);

        let texture: Texture2D = ResourceManager.getCardTexture(index);

        let baseMat: Material = ResourceManager.getBaseCardMat();
        baseMat.setProperty('mainTexture', texture);

        let kingMat: Material = ResourceManager.getKingCardMat();
        kingMat.setProperty('mainTexture', texture);
    }

}